package com.delivery.deliverysystem.util;

import java.io.File;
import java.util.ArrayList;

/**
 * 文件工具
 * @author LinHj
 * @date 2021/5/20 15:11
 **/
public class FileUtils {
    private static ArrayList<String> scanFiles = new ArrayList<>();

    public static ArrayList<String> getScanFiles(String folderPath){
        scanFiles.clear();
        scanFilesWithRecursion(folderPath);
        return scanFiles;
    }
    public static ArrayList<String> scanFilesWithRecursion(String folderPath) {
        ArrayList<String> dirctorys = new ArrayList<String>();
        File directory = new File(folderPath);
        if(!directory.isDirectory()){
            throw new RuntimeException("不是一个合法的文件夹");
        }
        if(directory.isDirectory()){
            File [] filelist = directory.listFiles();
            for(int i = 0; i < filelist.length; i ++){
                /**如果当前是文件夹，进入递归扫描文件夹**/
                if(filelist[i].isDirectory()){
                    dirctorys.add(filelist[i].getAbsolutePath());
                /**递归扫描下面的文件夹**/
                    scanFilesWithRecursion(filelist[i].getAbsolutePath());
                }
                /**非文件夹**/
                else{
                    scanFiles.add(filelist[i].getAbsolutePath());
                }
            }
        }
        return scanFiles;
    }
}
